package com.lmxdawn.api.admin.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lmxdawn.api.admin.annotation.AuthRuleAnnotation;
import com.lmxdawn.api.admin.req.ListPageRequest;
import com.lmxdawn.api.common.res.BaseResponse;
import com.lmxdawn.api.common.util.ResultVOUtils;
import com.lmxdawn.api.admin.entity.Training;
import com.lmxdawn.api.admin.service.ITrainingService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;

/**
 * <p>
 * 代练服务 前端控制器
 * </p>
 *
 * @author huangyubin
 * @since 2024-07-05
 */
@RestController
@RequestMapping("/admin/dnf/training")
public class TrainingController {

    @Resource
    ITrainingService trainingService;


    @GetMapping("/page")
    @AuthRuleAnnotation("admin/dnf/training/page")
    public BaseResponse page(@Valid ListPageRequest pageRequest) {

        return ResultVOUtils.success(trainingService.page(Page.of(pageRequest.getPage(), pageRequest.getLimit()),
                Wrappers.<Training>lambdaQuery().orderByDesc(Training::getCreateTime)
                ));
    }

    @PostMapping("/save")
    @AuthRuleAnnotation("admin/dnf/training/save")
    public BaseResponse save(@Valid @RequestBody Training training) {
        return ResultVOUtils.success(trainingService.save(training));
    }

    @PostMapping("/edit")
    @AuthRuleAnnotation("admin/dnf/training/edit")
    public BaseResponse edit(@Valid @RequestBody Training training) {
        return ResultVOUtils.success(trainingService.updateById(training));
    }

    @PostMapping("/delete")
    @AuthRuleAnnotation("admin/dnf/training/delete")
    public BaseResponse delete(@Valid @RequestBody Training training) {
        return ResultVOUtils.success(trainingService.removeById(training));
    }
}
